<template>
  <div class="major-type" v-loading.fullscreen.lock="loading">
    <page-main :pagination="pagination" @search-data="searchData" @page-change="pageChange">
      <template slot="search">
        <search-form :search="search"></search-form>
      </template>
      <template slot="append">
        <el-button type="primary" size="medium" @click="addMajor">新增</el-button>
      </template>
      <template slot="table">
        <el-table fit stripe border highlight-current-row :data="majorList" height="100%">
          <el-table-column prop="majorTypeId" label="类型id" min-width="120px" align="center"></el-table-column>
          <el-table-column prop="majorName" label="类型名称" min-width="120px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" label="操作" width="80px" align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="updateMajor(scope.row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </page-main>

    <el-dialog :title="dialogTitle[dialogType]" custom-class="sys-dialog" :close-on-click-modal="false" :visible.sync="openDialog" width="450px"
      top="calc(50vh - 80px)" @closed="dialogClosed">
      <div style="height:100px;overflow:auto">
        <major-form v-if="openDialog" :major="selectMajor" :type="dialogType" @save-success="saveSuccess"></major-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import BranchApi from 'services/branchApi';

  import PageMain from 'commonComponents/pageMain';
  import SearchForm from './searchForm';
  import MajorForm from './majorForm';

  import Pagination from 'models/pagination';
  import MajorTypeSearch from 'models/branch/majorTypeSearch';

  export default {
    data() {
      return {
        loading: false,
        search: new MajorTypeSearch(),
        pagination: new Pagination(),
        majorList: [],
        openDialog: false,
        dialogType: '',
        selectMajor: null,
        dialogTitle: {
          add: "新增主营类型",
          update: "修改主营类型"
        }
      };
    },

    created() {
      this.setMajorList(true);
    },

    methods: {
      setMajorList(isSearch = false) {
        let search = this.search;
        if (!isSearch) {
          search.resetOldData();
        }

        let pagination = this.pagination;
        this.loading = true;
        BranchApi.getAllMajorList(search, pagination).then(majorList => {
          if (isSearch) {
            search.setOldData();
          }

          if (majorList.length === 0 && pagination.currentPage > 1) {
            this.$nextTick(() => {
              this.setMajorList(isSearch);
            });
            return;
          }

          this.majorList = majorList;
          this.loading = false;
        }).catch(() => {
          this.loading = false;
        });
      },

      searchData() {
        this.pagination.currentPage = 1;
        this.setMajorList(true);
      },

      pageChange() {
        this.setMajorList();
      },

      addMajor() {
        this.dialogType = 'add';
        this.openDialog = true;
      },

      updateMajor(major) {
        this.dialogType = 'update';
        this.selectMajor = major;
        this.openDialog = true;
      },

      saveSuccess() {
        this.openDialog = false;
        this.setMajorList();
      },

      dialogClosed() {
        this.dialogType = '';
        this.selectMajor = null;
      }
    },

    components: {
      PageMain,
      SearchForm,
      MajorForm
    }
  };

</script>

<style lang="scss" scoped>
  .major-type {
    height: 100%;
  }

</style>
